package org.example.auth.service.userStrategy;

import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboReference;
import org.example.api.system.RemoteSystemUserService;
import org.example.auth.service.IAuthUserStrategy;
import org.example.common.core.constants.Constants;
import org.example.common.core.domain.model.LoginSocial;
import org.example.common.core.domain.model.LoginSocialConfig;
import org.example.common.core.domain.model.LoginUser;
import org.example.common.social.config.properties.SocialProperties;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 系统用户策略
 */

@Component(Constants.SYSTEM+IAuthUserStrategy.BASE_NAME)
@RequiredArgsConstructor
public class SysUserAuthStrategy implements IAuthUserStrategy {

    @DubboReference
    private RemoteSystemUserService systemUserService;
    private final SocialProperties socialProperties;

    @Override
    public LoginUser getLoginUserByUsername(String tenantId, String username) {
        return systemUserService.getSysUserByUsername(tenantId,username);
    }

    @Override
    public LoginUser getLoginUserByUserId(String tenantId, Long userId) {
        return systemUserService.getSysUserByUserId(tenantId,userId);
    }

    @Override
    public LoginSocial getLoginSocialByAuthId(String tenantId, String authId) {
        return systemUserService.getSocialByAuthId(tenantId,authId);
    }

    @Override
    public Map<String, LoginSocialConfig> getLoginSocialConfig(String tenantId) {
        Map<String, LoginSocialConfig> result=new ConcurrentHashMap<>();
        socialProperties.getType().forEach((k,v)->{
            LoginSocialConfig config=new LoginSocialConfig();
            BeanUtils.copyProperties(v,config);
            result.put(k,config);
        });
        return result;
    }

    @Override
    public LoginUser getLoginUserByPhone(String tenantId, String phone) {
        return systemUserService.getSysUserByPhone(tenantId,phone);
    }

    @Override
    public LoginUser getLoginUserByEmail(String tenantId, String email) {
        return systemUserService.getSysUserByEmail(tenantId,email);
    }
}
